﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Entity;
using System.Data;

namespace GroupChat2013.Data
{
    /// <summary>
    /// 最近登录用户存储类
    /// </summary>
    public class RecLoginRepository : BaseRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public RecLoginRepository()
            : base("t_RecLogin")
        {

        }

        /// <summary>
        /// 创建表
        /// </summary>
        protected override void CreateTable()
        {
            string cmdText = @"CREATE TABLE " + TableName +
                " (Id integer primary key autoincrement,UserId varchar(64),UserName nvarchar(64), Password nvarchar(64),Rememberpw integer,AutoLogin integer,LastLoginTime datetime);";
            SQLiteHelper.ExecuteNonQuery(DBPath, cmdText);
        }

        /// <summary>
        /// 获取最近登录过的用户
        /// </summary>
        /// <returns></returns>
        public List<RecLoginInfo> GetRecLoginInfo(int count)
        {
            string cmdText = "SELECT * FROM " + TableName + " order by lastlogintime desc limit " + count;

            List<RecLoginInfo> result = new List<RecLoginInfo>();
            var cmd = SQLiteHelper.CreateCommand(DBPath, cmdText);
            using (IDataReader reader = SQLiteHelper.ExecuteReader(cmd, cmdText, null))
            {
                while (reader.Read())
                {
                    result.Add(new RecLoginInfo()
                    {
                        Id = reader.GetInt32(reader.GetOrdinal("Id")),
                        UserId = reader.GetString(reader.GetOrdinal("UserId")),
                        UserName = reader.GetString(reader.GetOrdinal("UserName")),
                        Password = reader.GetString(reader.GetOrdinal("Password")),
                        Rememberpw = reader.GetBoolean(reader.GetOrdinal("Rememberpw")),
                        AutoLogin = reader.GetBoolean(reader.GetOrdinal("AutoLogin")),
                        LastLoginTime = reader.GetDateTime(reader.GetOrdinal("LastLoginTime"))
                    });
                }
            }
            return result;
        }

        /// <summary>
        /// 添加最近登录信息
        /// </summary>
        /// <param name="recLoginInfo"></param>
        public void AddRecLoginInfo(RecLoginInfo recLoginInfo)
        {
            if (recLoginInfo == null)
                return;

            if (IsExistByUserName(recLoginInfo.UserName))
            {
                string delText = "delete from " + TableName + " where UserName=@UserName";
                SQLiteHelper.ExecuteNonQuery(DBPath, delText, recLoginInfo.UserName);
            }

            string cmdText = @"INSERT INTO " + TableName + @" (UserId, UserName, Password, Rememberpw, AutoLogin,LastLoginTime) 
                                VALUES (@UserId, @UserName, @Password, @Rememberpw, @AutoLogin,@LastLoginTime)";

            SQLiteHelper.ExecuteNonQuery(DBPath, cmdText,
                                         recLoginInfo.UserId,
                                         recLoginInfo.UserName,
                                         recLoginInfo.Password,
                                         recLoginInfo.Rememberpw,
                                         recLoginInfo.AutoLogin,
                                         DateTime.Now);
        }

        /// <summary>
        /// 根据用户名查询用户是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private bool IsExistByUserName(string userName)
        {
            string cmdText = "SELECT count(*) FROM " + TableName + " where UserName=@UserName";
            object data = SQLiteHelper.ExecuteScalar(DBPath, cmdText, userName);
            return Convert.ToInt32(data) > 0;
        }
    }
}
